const app = getApp()
const api = require('../../../api/index.js')
const util = require("../../../utils/util.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    labelSelet:[
      {
        name:'个人提升',
        id:1,
        activ:true
      },
      {
        name: '亲子教学',
        id: 2,
        activ: false
      },
      {
        name: '团队教学',
        id: 3,
        activ: false
      },
      {
        name: '场馆教练',
        id: 4,
        activ: false
      }
    ],
    issueClass: '',
    catalogSelect: 0,
    img_arr: [],
    arr: [],
    formdata: '',
    imgI: [],
    sub: false,
    inputs:'',
    getTitle:'',
    isData:[],
    label_id:'',
    texts:'',
    c_id:'',
    address:'',
    community_id:'',
    slow:true
  },
  getTitle:function(e){ //文章标题
    var val = e.detail.value;
    console.log(val)
        this.setData({
          getTitle: val
        });
  },
  upimg: function () {
    var that = this;

    if (this.data.img_arr.length < 9) {
      console.log(456789)
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } 

  },
  deletes: function (e) {
    let index_img = e.target.dataset.index;
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var imgList = this.data.img_arr;
    var arr = that.data.arr;
    imgList.splice(index, 1);
    arr.splice(index, 1);
    var num = that.data.i - 1
    console.log(imgList)
    that.setData({
      img_arr: imgList,
      arr: arr,
      imgI: num, 
    });
  },
  inputs: function (e) { //文章内容
    // 获取输入框的内容
    var value = e.detail.value;
      this.setData({
        texts: value
      })
  },
  labSelet:function(e){
      let id = e.target.dataset.id
      let index = e.target.dataset.in
      let thata = this;
      this.data.isData.company_age.forEach( (item,index) =>{
        let key = `isData.company_age.[${index}].activ`
        thata.setData({
          [key]:false
        })  
      })
      this.setData({
        [`isData.company_age.[${index}].activ`]:true,
        label_id: id
      })
    console.log(this.data.label_id)
  },
  bindPickerChange: function(e) {
    let cid = this.data.isData.classify[e.detail.value].id
    this.setData({
      index: e.detail.value,
      c_id: cid
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Sport()
   
    let id = options.id
    console.log(options)
    this.setData({
      address: options.sipk,
      community_id: id
    })
    
  },
  Sport:function(){
    api.Sport( (res)=>{
      console.log(res)
      this.setData({
        isData: res.data.result
      })
    })
  },
  releaseData:function(){
    let that = this
    if (this.data.img_arr.length == 0){
      wx.showToast({
        title: '亲，必须要上传图片哦~',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (this.data.img_arr.length > 9) {
      wx.showToast({
        title: '亲，图片上传不能多余9张~',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (this.data.getTitle.length <5){
        wx.showToast({
          title: '亲，字数要5个字以上哦~',
          icon: 'none',
          duration: 2000
        })
        return false
    }
    if (this.data.texts.length < 10 && this.data.texts.length >=200) {
      wx.showToast({
        title: '亲，内容要50字以上哦~ 200以内',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (this.data.label_id == '') {
      wx.showToast({
        title: '亲，要选择标签哦~',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    console.log(445555)
    if (this.data.c_id == '') {
      wx.showToast({
        title: '亲，还有运动没有选哦~',
        icon: 'none',
        duration: 2000
      })
      return false
    }
      let data ={
        c_id: that.data.c_id, //分类ID
        note_title: that.data.getTitle, //文章标题
        note_desc: that.data.texts, //文章内容,
        label_id: that.data.label_id,// 分类id
        longitude: wx.getStorageSync('key').longitude, //经度
        latitude: wx.getStorageSync('key').latitude, //经度
        community_id: that.data.community_id,//商圈id
      }

    if (this.data.slow != true){
      return false
    }
    console.log(123456789)
    api.addNote(data,(res)=>{
      console.log(456789789)
      console.log(res.data.result.note_id)
          let id = res.data.result.note_id
          this.upload(id)
          this.setData({
            slow:false
          })
    })
  },
  upload: function (id) {
    var that = this
    console.log(id)
    for (var i = 0; i < this.data.img_arr.length; i++) {
      console.log(that.data.img_arr[i])
      wx.uploadFile({
        url:  'https://www.didigolf.top/Community/Community/addNote',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token').token
        },
        filePath: that.data.img_arr[i],
        name: 'url',
        formData: {
          note_id: id,
        },
        success: function (res) {
          console.log(res)
          that.setData({
            slow: true
          })
          that.setData({
            c_id: '', //分类ID
            note_title: '', //文章标题
            note_desc: '', //文章内容,
            label_id: '',// 分类id
            img_arr: ''
          })
        },
        fail: function (res) {
         console.log(res)
        }
      })
    }
    this.setData({
      formData: ''
    })
    wx.showToast({
      title: '等待审核',
      duration: 1000
    });
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 1000)
    
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

})